cmake_minimum_required(VERSION 2.8.8)
project(libe57_metadata)

set (libe57_metadata_VERSION_MAJOR 0)
set (libe57_metadata_VERSION_MINOR 1)

include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_BINARY_DIR}")
include_directories("${PROJECT_SOURCE_DIR}/include")
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)

if(CMAKE_COMPILER_IS_GNUCXX)
	add_definitions(-fPIC)
	add_definitions(-O3)
	add_definitions(-g)
	add_definitions(-std=c++1y)
endif()

find_package(Eigen)
if(MSVC)
	set(Xerces_USE_STATIC_LIBS On)
	set(Boost_USE_STATIC_LIBS On)
else(MSVC)
	set(Xerces_USE_STATIC_LIBS Off)
	set(Boost_USE_STATIC_LIBS Off)
endif(MSVC)
#set(Boost_USE_STATIC_RUNTIME ON)
find_package(Boost COMPONENTS system filesystem)
find_package(Cereal)
find_package(E57Ref)
find_package(E57Simple)
find_package(Xerces)
if (Xerces_USE_STATIC_LIBS)
add_definitions(-DXERCES_STATIC_LIBRARY)
endif()

if(MSVC)
file(TO_NATIVE_PATH "${CMAKE_SOURCE_DIR}" WIN_SOURCE_DIR)
message(STATUS "windir: ${WIN_SOURCE_DIR}")
configure_file(e57_metadata_wrapper/e57_metadata_wrapper.csproj "${CMAKE_BINARY_DIR}/e57_metadata_wrapper.csproj")
include_external_msproject(
	e57_metadata_wrapper
	${CMAKE_BINARY_DIR}/e57_metadata_wrapper.csproj
	TYPE FAE04EC0-301F-11D3-BF4B-00C04F79EFBC
)
endif(MSVC)

file (GLOB_RECURSE cpplibh RELATIVE "${PROJECT_SOURCE_DIR}" "e57_metadata/*.hpp")
file (GLOB_RECURSE cpplib RELATIVE "${PROJECT_SOURCE_DIR}" "e57_metadata/*.cpp")
file (GLOB_RECURSE testcpp RELATIVE "${PROJECT_SOURCE_DIR}" "test_cpplib/*.cpp")
if (Boost_FOUND AND EIGEN_FOUND AND CEREAL_FOUND AND E57REF_FOUND AND E57SIMPLE_FOUND AND Xerces_FOUND)
	include_directories(${EIGEN_INCLUDE_DIRS})
	include_directories(${CEREAL_INCLUDE_DIRS})
	include_directories(${Boost_INCLUDE_DIRS})
	include_directories(${E57REF_INCLUDE_DIRS})
	include_directories(${E57SIMPLE_INCLUDE_DIRS})
	link_directories(${E57SIMPLE_LIBRARY_DIRS})
	link_directories(${Boost_LIBRARY_DIRS})
	add_library(e57_metadata SHARED ${cpplib} ${cpplibh})
	if(CMAKE_COMPILER_IS_GNUCXX)
		target_link_libraries(e57_metadata ${Boost_LIBRARIES} ${E57SIMPLE_LIBRARIES} ${Xerces_LIBRARY} "dl")
	else()
		target_link_libraries(e57_metadata ${Boost_LIBRARIES} ${E57REF_LIBRARIES} ${Xerces_LIBRARY})
	endif()
	add_executable(test_cpplib ${testcpp})
	if(CMAKE_COMPILER_IS_GNUCXX)
		# MH: I had to tinker around with linker errors on linux, because symbols from the the statically build 
		# E57RefImpl library where not imported. The following '--whole-archive' magic solves the issue.
		target_link_libraries(test_cpplib e57_metadata -Wl,--whole-archive ${E57REF_LIBRARIES} -Wl,--no-whole-archive)
	else()
		target_link_libraries(test_cpplib e57_metadata)
	endif()
	

	# install binary
	install (TARGETS e57_metadata DESTINATION bin)
	install (TARGETS test_cpplib DESTINATION bin)
	if (MSVC)
	install (FILES "${CMAKE_BINARY_DIR}/bin/Release/e57_metadata_wrapper.exe" DESTINATION bin)
	endif(MSVC)
	# install header
	install (FILES ${cpplibh} DESTINATION include)
	install (FILES "e57_metadata_wrapper/e57_metadata_wrapper.cs" DESTINATION include)
else()
	if (NOT Boost_FOUND)
		message(STATUS "Boost has not been found")
	endif()
	if (NOT EIGEN_FOUND)
		message(STATUS "Eigen has not been found")
	endif()
	if (NOT CEREAL_FOUND)
		message(STATUS "Cereal has not been found")
	endif()
	if (NOT E57REF_FOUND)
		message(STATUS "e57lib has not been found")
	endif()
	if (NOT XERCES_FOUND)
		message(STATUS "Xerces has not been found")
	endif()
endif()

